package org.lql.shop.cloud.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.lql.shop.cloud.IOrderService;
import org.lql.shop.cloud.entity.ShopOrderEntity;
import org.lql.shop.cloud.mapper.ShopOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * @author: lql
 * @date: 2022/7/2 14:38
 * @description:
 */
@DubboService(group = "jike-shop", version = "1.0.0", protocol = "dubbo")
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String createOrder(Long orderId) {
        log.info("订单接口 ======> ");
        if (Boolean.TRUE.equals(redisTemplate.hasKey(orderId))) {
            log.info("订单接口,重复下单 ===> {}", orderId);
            return "failed ===> repeat";
        } else {
            String orderCode = UUID.randomUUID().toString();
            ShopOrderEntity shopOrderEntity = new ShopOrderEntity();
            shopOrderEntity.setOrderId(orderId);
            shopOrderEntity.setOrderCode(orderCode);
            shopOrderMapper.insert(shopOrderEntity);
            log.info("下单成功 =====> {}", orderCode);
            redisTemplate.opsForValue().set(orderId, orderCode,10, TimeUnit.DAYS);
        }
        return "success";
    }
}
